﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ConferenceRegister.Domain;
using ConferenceRegister.Models;
using ConferenceRegister.Mailers;

namespace ConferenceRegister.Controllers
{
    [Authorize]
    public class AdminController : Controller
    {
        RegisterService service = new RegisterService();
        UserMailer Mailer = new UserMailer(System.Web.HttpContext.Current);
        

        public ActionResult List()
        { 
            AdminModel model = new AdminModel();
            List<Conference> conferences = service.All.Conferences.All.OrderByDescending(e=>e.EndDate).ToList();
            model.Conferences = new SelectList(conferences, "ID", "Name", conferences[0]);
            model.SelectedConferenceID = conferences[0].ID;
            model.Matches = service.Registrants(conferences[0].ID).OrderByDescending(e=>e.SubmitDate).ToList();
            model.SelectedConference = conferences[0];
            return View(model); 
        }

        [HttpPost]
        public ActionResult List(AdminModel model)
        {
            if (ModelState.IsValid)
            {
                model.Matches = service.Registrants(model.SelectedConferenceID);
                List<Conference> conferences = service.All.Conferences.All.OrderByDescending(e => e.EndDate).ToList();
                model.Conferences = new SelectList(conferences, "ID", "Name", conferences[0]);
                model.SelectedConference = service.All.Conferences.Find(model.SelectedConferenceID);
                return View(model);
            }

            return RedirectToAction("List");
        }

        public ActionResult EditRegistrant(int id)
        {
            return View(service.All.Registrants.Find(id));
        }

        [HttpPost]
        public ActionResult EditRegistrant(Register r, bool IsDelivered)
        {

            if (ModelState.IsValid)
            {
                r.FixPhoneNumbers();
                if (IsDelivered && r.DeliverDate == null)
                    r.DeliverDate = DateTime.UtcNow;
                else if (!IsDelivered)
                    r.DeliverDate = null;
                decimal cost = service.All.Conferences.Find(r.ConferenceID).Cost;
                if (r.PaymentAmount >= cost && r.PaymentDate == null)
                    r.PaymentDate = DateTime.UtcNow;
                else if (r.PaymentAmount < cost)
                    r.PaymentDate = null;

                service.All.Registrants.Update(r);
                return RedirectToAction("List");
            }
            return View(r);
        }

        [HttpPost]
        public ActionResult ConfirmationEmail(int id, string emails)
        {
            List<string> e = emails.Split(new string[]{" ", ",", "\r\n", "|", ";"}, StringSplitOptions.RemoveEmptyEntries).ToList();
			Register r = service.All.Registrants.Find(id);
            Mailer.ConfirmRegistration(r, e);
			return View("Confirm", r);
        }
    }
}
